package edu.msg.hrappy.backend.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;

@Entity
@Table(name = "Job")
public class Job extends BaseEntity {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private String name;
    private int nrOfPositions;
    private User propPers;
    private DepartmentType department;
    private String project;
    private String requirements;
    private String responsibilities;
    private Date deadline;
    private boolean open;
    private List<Candidate> candidates = new ArrayList<Candidate>();
    private List<Comment> comments = new ArrayList<Comment>();
    private int cod;
    private String uniqueCode;
    private Boolean active;
    private List<User> involvedUsers = new ArrayList<User>();

    public Job() {
    }

    public Job(String name, int nrOfPositions, User propPers,
            DepartmentType department, String project, String requirements,
            String responsibilities, Date deadline, boolean open,
            List<Candidate> candidates, int cod, List<Comment> comments) {

        this.name = name;
        this.nrOfPositions = nrOfPositions;
        this.propPers = propPers;
        this.department = department;
        this.project = project;
        this.requirements = requirements;
        this.responsibilities = responsibilities;
        this.deadline = deadline;
        this.open = open;
        this.candidates = candidates;
        this.comments = comments;
        this.cod = cod;
        this.uniqueCode = name + "_" + department + "_" + deadline + "_" + cod;
        this.active = false;
    }

    public Job(Long id, String name, int nrOfPositions, User propPers,
            DepartmentType department, String project, String requirements,
            String responsibilities, Date deadline, boolean open,
            List<Candidate> candidates, int cod, List<Comment> comments) {
        super(id);
        this.name = name;
        this.nrOfPositions = nrOfPositions;
        this.propPers = propPers;
        this.department = department;
        this.project = project;
        this.requirements = requirements;
        this.responsibilities = responsibilities;
        this.deadline = deadline;
        this.open = open;
        this.candidates = candidates;
        this.comments = comments;
        this.cod = cod;
        this.uniqueCode = name + "_" + department + "_" + deadline + "_" + cod;
        this.active = false;
    }

    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "nrOfPositions")
    public int getNrOfPositions() {
        return nrOfPositions;
    }

    public void setNrOfPositions(int nrOfPositions) {
        this.nrOfPositions = nrOfPositions;
    }

    @OneToOne(cascade = {CascadeType.ALL})
    @JoinColumn(name = "USER_ID")
    public User getPropPers() {
        return propPers;
    }

    public void setPropPers(User propPers) {
        this.propPers = propPers;
    }

    @OneToOne
    @JoinColumn(name = "DEPARTMENT_ID")
    public DepartmentType getDepartment() {
        return department;
    }

    public void setDepartment(DepartmentType department) {
        this.department = department;
    }

    @Column(name = "project")
    public String getProject() {
        return project;
    }

    public void setProject(String project) {
        this.project = project;
    }

    @Column(name = "requirements")
    public String getRequirements() {
        return requirements;
    }

    public void setRequirements(String requirements) {
        this.requirements = requirements;
    }

    @Column(name = "responsibilities")
    public String getResponsibilities() {
        return responsibilities;
    }

    public void setResponsibilities(String responsibilities) {
        this.responsibilities = responsibilities;
    }

    @Temporal(javax.persistence.TemporalType.DATE)
    @Column(name = "deadline")
    public Date getDeadline() {
        return (deadline == null) ? null : (Date) deadline.clone();
    }

    public void setDeadline(Date deadline) {
        this.deadline = deadline;
    }

    @Column(name = "open")
    public boolean isOpen() {
        return open;
    }

    public void setOpen(boolean open) {
        this.open = open;
    }

    @OneToMany(mappedBy = "job")
    @JoinColumn(name = "JOB_ID")
    public List<Candidate> getCandidates() {
        return candidates;
    }

    public void setCandidates(List<Candidate> candidates) {
        this.candidates = candidates;
    }

    @OneToMany(cascade = {CascadeType.ALL})
    @JoinColumn(name = "JOB_ID")
    public List<Comment> getComments() {
        return comments;
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }

    @Column(name = "cod")
    public int getCod() {
        return cod;
    }

    public void setCod(int cod) {
        this.cod = cod;
    }

    @Column(name = "uniqueCode")
    public String getUniqueCode() {
        return uniqueCode;
    }

    public void setUniqueCode(String uniqueCode) {
        this.uniqueCode = uniqueCode;
    }

    public void setComment(Comment comment) {
        this.comments.add(comment);
    }

    public void setCandidate(Candidate candidate) {
        this.candidates.add(candidate);
    }
    
    public void removeCandidate(Candidate candidate) {
    	this.candidates.remove(candidate);
    }

    @Column(name = "active")
    public Boolean isActive() {
        return active;
    }
    
    public Boolean getActive() {
        return active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }

    @ManyToMany
    @JoinTable(name = "JOB_USER", joinColumns = {
        @JoinColumn(name = "JOB_ID")}, inverseJoinColumns = {
        @JoinColumn(name = "USER_ID")})
    public List<User> getInvolvedUsers() {
        return involvedUsers;
    }

    public void setInvolvedUsers(List<User> involvedUsers) {
        this.involvedUsers = involvedUsers;
    }

    
    public void addInvolvedUser(User user) {
        this.involvedUsers.add(user);
    }
    public void removeInvolvedUser(User user) {
        this.involvedUsers.remove(user);
    }

	public void removeComment(Comment comment) {
		this.comments.remove(comment);
		
	}
}
